import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { NationalTreasureModule } from "../../../modules/NationalTreasureModule";
import { NationalTreasureIcon } from "../view/nationalTreasure/NationalTreasureIcon";
import { NationalTreaIcon } from "../view/nationalTreasure/NationalTreaIcon";
import { NationalTreaScoreView } from "../view/nationalTreasure/NationalTreaScoreView";
import { NationalTreasureWindow } from "../view/nationalTreasure/NationalTreasureWindow";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { EffectManager } from "../manager/EffectManager";
import { UIIconEvent } from "../view/uiIconBtn/UIIconEvent";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { EEntityFightMode } from "../../../Message/Public/EEntityFightMode";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { ItemConfig } from "../resource/ItemConfig";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { INationalTreasureModule } from "../../../modules/interfaces/INationalTreasureModule";
type int = number;
//class NationalTreasureController
    
    export  class NationalTreasureController extends Controller
    {
       

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            this.showScoreWindow();
            if (this._nationalTreaWin && !this._nationalTreaWin.isHide) 
            {
                if (cache.nationalTrea.nationalInfo.hasCheckIn && cache.nationalTrea.nationalInfo.isSceneOpen(MapFileUtil.mapID)) 
                {
                    this.nationalTreaWin.hide();
                }
            }
            if (this._nationalLightIcon && !this._nationalLightIcon.isHide) 
            {
                this._nationalLightIcon.changeScene();
            }
            return;
        }

        /* internal  */onNationalTreaIconClickHandler(arg1: DataEvent): void
        {
            this.nationalTreaWin.updateData(cache.nationalTrea.nationalInfo);
            this.nationalTreaWin.show();
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.NationalTreasure1OptOpen, this.onNationalTreaOpen1Handler);
            Dispatcher.addEventListener(EventName.NationalTreasure2OptOpen, this.onNationalTreaOpen2Handler);
            Dispatcher.addEventListener(EventName.NationalTreasureWindowClose, this.onCheckInWindowCloseHandler);
            Dispatcher.addEventListener(EventName.NationalTreasureWindowShowReq, this.onCheckInWindowShowReqHandler);
            Dispatcher.addEventListener(EventName.NationalTreasureIconClick, this.onNationalTreaIconClickHandler);
            Dispatcher.addEventListener(EventName.NationalTreasureShowDetial, this.onNationalTreasureShowDetialHandler);
            Dispatcher.addEventListener(EventName.NationalTreasureJoinReq, this.onJoinReqHandler);
            Dispatcher.addEventListener(EventName.NationalTreasurePassReq, this.onPassToReqHandler);
            Dispatcher.addEventListener(EventName.NationalTreasurePassNPC, this.onNPCPassToReqHandler);
            Dispatcher.addEventListener(EventName.NationalTreasureHandOutReq, this.onHandOutReqHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureSignUpStart, this.onSingUpStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureSignUpUpdate, this.onSingUpUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasurePlaySignUpdate, this.onJoinResHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureActiveStart, this.onActiveStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureActiveEnd, this.onActiveEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureCampInfoUpdate, this.onCampInfoUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.NationalTreasureHonorUpdate, this.onHonorInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new NationalTreasureModule();
            return loc1;
        }

        public get nationalTreaIcon(): NationalTreasureIcon
        {
            if (!this._nationalTreaIcon) 
            {
                this._nationalTreaIcon = new NationalTreasureIcon();
                this._nationalTreaIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onNationalTreaClickHandler);
            }
            return this._nationalTreaIcon;
        }

        public get nationalLightIcon(): NationalTreaIcon
        {
            if (!this._nationalLightIcon) 
            {
                this._nationalLightIcon = NationalTreaIcon.instance;
            }
            return this._nationalLightIcon;
        }

        public get nationalScoreWin(): NationalTreaScoreView
        {
            if (!this._nationalScoreWin) 
            {
                this._nationalScoreWin = NationalTreaScoreView.instance;
            }
            return this._nationalScoreWin;
        }

        public get nationalTreaWin(): NationalTreasureWindow
        {
            if (!this._nationalTreaWin) 
            {
                this._nationalTreaWin = NationalTreasureWindow.instance;
            }
            return this._nationalTreaWin;
        }

        /* internal  */onNationalTreaClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.nationalTreaWin.updateData(cache.nationalTrea.nationalInfo);
            this.nationalTreaWin.show();
            return;
        }

        /* internal  */showCheckInIcon(arg1: Boolean=true): void
        {
            if (!cache.nationalTrea.levelReach()) 
            {
                return;
            }
            if (!this.nationalTreaIcon.parent) 
            {
                UiHintIconView.instance.addHintIcon(this.nationalTreaIcon, arg1);
            }
            return;
        }

        /* internal  */hideCheckInIcon(): void
        {
            if (this._nationalTreaIcon && this._nationalTreaIcon.parent) 
            {
                this._nationalTreaIcon.parent.removeChild(this._nationalTreaIcon);
                EffectManager.glowFilterUnReg(this._nationalTreaIcon);
                this._nationalTreaIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
            }
            return;
        }

        /* internal  */showScoreWindow(): void
        {
            if (cache.nationalTrea.nationalInfo.hasCheckIn && cache.nationalTrea.nationalInfo.checkInStart && cache.nationalTrea.nationalInfo.isSceneOpen(MapFileUtil.mapID)) 
            {
                this.nationalScoreWin.show();
                this.nationalScoreWin.updateData(cache.nationalTrea.nationalInfo);
            }
            else if (this._nationalScoreWin && this._nationalScoreWin.parent) 
            {
                this._nationalScoreWin.hide();
            }
            return;
        }

        /* internal  */onJoinReqHandler(arg1: DataEvent): void
        {
            if (RolePlayer.instance.entityInfo.entityInfo.fightModel == EEntityFightMode._EEntityFightModePeace) 
            {
                if (cache.nationalTrea.nationalInfo.isSceneOpen(MapFileUtil.mapID)) 
                {
                    Alert.buttonWidth = 80;
                    Alert.show(Language.getString(27061), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onJoinReqAlert);
                    return;
                }
            }
            GameProxy.nationalTreasure.join();
            return;
        }

        /* internal  */onJoinReqAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.nationalTreasure.join();
            }
            return;
        }

        /* internal  */onPassToReqHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            if (cache.nationalTrea.nationalInfo.isStartTime() && cache.nationalTrea.inActiveScene() && cache.nationalTrea.nationalInfo.isCampOpen(loc1)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27066));
                return;
            }
            this.passToCheck(loc1);
            return;
        }

        /* internal  */onNPCPassToReqHandler(arg1: DataEvent): void
        {
            this.passToCheck(int(arg1.data));
            return;
        }

        /* internal  */passToCheck(arg1: int): void
        {
            var loc1=null;
            if (cache.nationalTrea.passMapCheckByCampId(arg1)) 
            {
                loc1 = cache.scene.getSceneInfoByCamp(arg1);
                Alert.extendObj = arg1;
                Alert.buttonWidth = 80;
                Alert.show(Language.getStringByParam(27062, loc1.sMapDefine.name), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.passAlertHandler);
                return;
            }
            GameProxy.nationalTreasure.passTo(arg1);
            return;
        }

        /* internal  */passAlertHandler(arg1: int, arg2: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.nationalTreasure.passTo(arg2);
            }
            return;
        }

        /* internal  */onHandOutReqHandler(arg1: DataEvent): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            if (PulseSharedObject.isTodayNotTips(cache.nationalTrea.todayNotTipsHandOut)) 
            {
                this.onAlertHandOutHandler(Alert.OK, false);
                return;
            }
            var loc1=cache.pack.backPackCache.getTaskItemCountByCategoryAndType(ECategory._ECategoryTask, EProp._EPropTreasureCrystal);
            var loc2=cache.pack.backPackCache.getTaskItemCountByCategoryAndType(ECategory._ECategoryTask, EProp._EPropTreasureOre);
            if (loc1 + loc2 > 0) 
            {
                loc3 = ItemConfig.instance.getItemByType(ECategory._ECategoryTask, EProp._EPropTreasureCrystal);
                loc4 = ItemConfig.instance.getItemByType(ECategory._ECategoryTask, EProp._EPropTreasureOre);
                if (loc3 && loc4) 
                {
                    loc5 = Language.getString(27033) + loc3.htmlName + Language.getStringByParam(27034, HTMLUtil.addColor(cache.nationalTrea.nationalInfo.rewardExp() + "", "#ffff00"), HTMLUtil.addColor(cache.nationalTrea.nationalInfo.rewardContribution() + "", "#ffff00")) + "，" + loc4.htmlName + Language.getStringByParam(27034, HTMLUtil.addColor(cache.nationalTrea.nationalInfo.rewardExp(true) + "", "#ffff00"), HTMLUtil.addColor(cache.nationalTrea.nationalInfo.rewardContribution(true) + "", "#ffff00")) + "。" + "<br>" + Language.getStringByParam(27035, loc3.htmlName + loc1 + "", loc4.htmlName + loc2 + "");
                    Alert.buttonWidth = 80;
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(loc5, Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onAlertHandOutHandler);
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(27031));
            }
            return;
        }

        /* internal  */onAlertHandOutHandler(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.nationalTreasure.handOut();
            }
            if (arg2) 
            {
                PulseSharedObject.save(cache.nationalTrea.todayNotTipsHandOut, true);
            }
            return;
        }

        public onSingUpStartHandler(arg1: Object): void
        {
            this.showCheckInIcon();
            this.showScoreWindow();
            this.nationalLightIcon.show();
            this.nationalLightIcon.updateData(cache.nationalTrea.nationalInfo);
            if (_view && !_view.isHide) 
            {
                (_view as INationalTreasureModule).updateData(cache.nationalTrea.nationalInfo);
            }
            if (this._nationalTreaWin && !this._nationalTreaWin.isHide) 
            {
                this.nationalTreaWin.updateData(cache.nationalTrea.nationalInfo);
            }
            return;
        }

        public onSingUpUpdateHandler(arg1: Object): void
        {
            if (this._nationalTreaWin && !this._nationalTreaWin.isHide) 
            {
                this.nationalTreaWin.joinUpdate(cache.nationalTrea.nationalInfo);
            }
            return;
        }

        public onJoinResHandler(arg1: Object): void
        {
            if (this._nationalTreaWin && !this._nationalTreaWin.isHide) 
            {
                this.nationalTreaWin.joinUpdate(cache.nationalTrea.nationalInfo);
            }
            if (_view && !_view.isHide) 
            {
                (_view as INationalTreasureModule).joinUpdate(cache.nationalTrea.nationalInfo);
            }
            this.hideCheckInIcon();
            this.showScoreWindow();
            if (this.nationalLightIcon.isHide) 
            {
                this.nationalLightIcon.show();
                this.nationalLightIcon.updateData(cache.nationalTrea.nationalInfo);
            }
            this.nationalScoreWin.updateData(cache.nationalTrea.nationalInfo);
            return;
        }

        public onActiveStartHandler(arg1: Object): void
        {
            this.nationalLightIcon.updateData(cache.nationalTrea.nationalInfo);
            if (cache.nationalTrea.nationalInfo.hasCheckIn) 
            {
                this.hideCheckInIcon();
            }
            this.showScoreWindow();
            return;
        }

        public onActiveEndHandler(arg1: Object): void
        {
            this.hideCheckInIcon();
            if (_view && !_view.isHide) 
            {
                (_view as INationalTreasureModule).updateData(cache.nationalTrea.nationalInfo);
            }
            if (this._nationalTreaWin && !this._nationalTreaWin.isHide) 
            {
                this.nationalTreaWin.updateData(cache.nationalTrea.nationalInfo);
            }
            if (this._nationalLightIcon && !this._nationalLightIcon.isHide) 
            {
                this._nationalLightIcon.hide();
            }
            if (cache.nationalTrea.nationalInfo.hasCheckIn && cache.scene.isMainCity()) 
            {
                Alert.buttonWidth = 100;
                Alert.show(Language.getString(27032), Language.getString(20055), Alert.OK, null, this.onActiveEndAlert);
            }
            if (this._nationalScoreWin && !this._nationalScoreWin.isHide) 
            {
                this._nationalScoreWin.updateData(cache.nationalTrea.nationalInfo);
            }
            return;
        }

        /* internal  */onActiveEndAlert(arg1: int): void
        {
            return;
        }

        /* internal  */onCampInfoUpdateHandler(arg1: Object): void
        {
            if (this._nationalScoreWin && this._nationalScoreWin.parent) 
            {
                this._nationalScoreWin.updateCampInfo(cache.nationalTrea.nationalInfo);
            }
            return;
        }

        /* internal  */onHonorInfoUpdateHandler(arg1: Object): void
        {
            if (this._nationalScoreWin && !this._nationalScoreWin.isHide) 
            {
                this._nationalScoreWin.updateHonor(int(arg1));
            }
            return;
        }

        /* internal  */onNationalTreaOpen1Handler(arg1: DataEvent): void
        {
            this.showNationalModule();
            return;
        }

        /* internal  */onNationalTreaOpen2Handler(arg1: DataEvent): void
        {
            this.showNationalModule();
            return;
        }

        /* internal  */onNationalTreasureShowDetialHandler(arg1: DataEvent): void
        {
            this.showNationalModule(false);
            return;
        }

        /* internal  */showNationalModule(arg1: Boolean=true): void
        {
            view.show();
            (view as INationalTreasureModule).updateData(cache.nationalTrea.nationalInfo);
            if (arg1) 
            {
                (view as INationalTreasureModule).showOptWin(true);
            }
            else 
            {
                (view as INationalTreasureModule).showOptWin(false);
            }
            return;
        }

        /* internal  */onCheckInWindowCloseHandler(arg1: DataEvent): void
        {
            if (cache.nationalTrea.nationalInfo.checkInStart && !cache.nationalTrea.nationalInfo.hasCheckIn) 
            {
                this.showCheckInIcon(false);
            }
            return;
        }

        /* internal  */onCheckInWindowShowReqHandler(arg1: DataEvent): void
        {
            if (this.nationalTreaWin.isHide) 
            {
                this.nationalTreaWin.updateData(cache.nationalTrea.nationalInfo);
                this.nationalTreaWin.show();
            }
            return;
        }

        private /* var */_nationalTreaIcon: NationalTreasureIcon;

        private /* var */_nationalLightIcon: NationalTreaIcon;

        private /* var */_nationalScoreWin: NationalTreaScoreView;

        private /* var */_nationalTreaWin: NationalTreasureWindow;
    }
